<?php
namespace Home\Controller;
use Think\Controller;
class uploadController extends Controller {
	public function index() {
		$this -> display();
	}

	public function upload() {
		$upload = new \Think\Upload();
		// 实例化上传类
		$upload -> maxSize = 3145728;
		// 设置附件上传大小
		$upload -> exts = array('jpg', 'gif', 'png', 'jpeg');
		// 设置附件上传类型
		$upload -> rootPath = './Uploads/';
		// 设置附件上传根目录
		$upload -> savePath = '';
		// 设置附件上传（子）目录
		// 上传文件
		$info = $upload -> upload();
		if (!$info) {// 上传错误提示错误信息
			$this -> error($upload -> getError());
		} else {// 上传成功
			$this -> success('上传成功！');
		}
	}

	public function uploadFile() {
		echo "123456";
		exit ;
		if (empty($_FILES) === false) {
			$save_path = 'file/';
			$save_url = 'file/';
			$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'html', 'text', 'txt', 'rar');
			$max_size = 1000000;

			header('Content-type: text/html; charset=UTF-8');

			$file_name = $_FILES['imgFile']['name'];
			$tmp_name = $_FILES['imgFile']['tmp_name'];
			$file_size = $_FILES['imgFile']['size'];

			if (!$file_name) { echo "请选择文件。";
			}
			if (is_dir($save_path) === false) { mkdir($save_path);
			}
			if (@is_writable($save_path) === false) { echo "上传目录没有写权限。";
			}
			if (@is_uploaded_file($tmp_name) === false) { echo "临时文件可能不是上传文件。";
			}
			if ($file_size > $max_size) { echo "上传文件大小超过限制。";
			}
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			if (in_array($file_ext, $ext_arr) === false) { echo "不允许的文件类型";
			} else {
				$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
				$file_path = $save_path . $new_file_name;
				if (move_uploaded_file($tmp_name, $file_path) === false) {
					echo "上传失败!";
				} else {
					echo '上传成功! --> ' . $file_url = $save_url . $new_file_name;
				}
			}

			exit ;
		}

		if (isset($_POST['chromeFile'])) {
			$save_path = 'file/';
			if (is_dir($save_path) === false) { mkdir($save_path);
			}
			$fdata = $_POST['chromeFile'];
			$ftype = substr($fdata, 0, strcspn($fdata, ',') + 1);
			$fdata = substr($fdata, strlen($ftype), strlen($fdata));
			$data = base64_decode($fdata);
			$ftype = substr($ftype, strcspn($ftype, '/') + 1, strcspn($ftype, ';') - strcspn($ftype, '/') - 1);

			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $ftype;
			file_put_contents($save_path . $new_file_name, $data);
			echo '上传成功! --> ' . $save_path . $new_file_name;

			exit ;
		}
		echo '上传成功! --> ';
	}

	function GetUserBrowserInfo($Agent) {
		$Agent = strtolower($Agent);
		//转小写
		$br_Name = '';
		//浏览器名称
		$br_Version = array();
		//浏览器的版本
		if (preg_match('/msie\s([0-9.0-9]+)/', $Agent, $br_Version))
			$br_Name = "Internet Explorer";
		else if (preg_match('/opera\/([0-9.0-9]+)/', $Agent, $br_Version))
			$br_Name = "Opera";
		else if (preg_match('/firefox\/([0-9.0-9]+)/', $Agent, $br_Version))
			$br_Name = "Firefox";
		else if (preg_match('/chrome\/([0-9.0-9]+)/', $Agent, $br_Version))
			$br_Name = "Chrome";
		else if (preg_match('/safari\/([0-9.0-9]+)/', $Agent, $br_Version))
			$br_Name = "Safari";
		else
			$br_Name = "Unknown";

		return $br_Name . " " . $br_Version[1];
	}

	function GetUserOSInfo($Agent) {
		$Agent = strtolower($Agent);
		//转小写
		$os_Name = '';
		if (preg_match('/win/', $Agent) && strpos($Agent, '95'))
			$os_Name = "Windows 95";
		else if (preg_match('/win 9x/', $Agent) && strpos($Agent, '4.90'))
			$os_Name = "Windows ME";
		else if (preg_match('/win/', $Agent) && preg_match('/98/', $Agent))
			$os_Name = "Windows 98";
		else if (preg_match('/win/', $Agent) && preg_match('/nt 5.0/', $Agent))
			$os_Name = "Windows 2000";
		else if (preg_match('/win/', $Agent) && preg_match('/nt 5.1/', $Agent))
			$os_Name = "Windows XP";
		else if (preg_match('/win/', $Agent) && preg_match('/nt 6.0/', $Agent))
			$os_Name = "Windows Vista";
		else if (preg_match('/win/', $Agent) && preg_match('/nt 6.1/', $Agent))
			$os_Name = "Windows 7";
		else if (preg_match('/win/', $Agent) && preg_match('/32/', $Agent))
			$os_Name = "Windows 32";
		else if (preg_match('/win/', $Agent) && preg_match('/nt/', $Agent))
			$os_Name = "Windows NT";
		else if (preg_match('/mac os/', $Agent))
			$os_Name = "Mac OS";
		else if (preg_match('/linux/', $Agent))
			$os_Name = "Linux";
		else if (preg_match('/unix/', $Agent))
			$os_Name = "Unix";
		else if (preg_match('/sun/', $Agent) && preg_match('/os/', $Agent))
			$os_Name = "SunOS";
		else if (preg_match('/ibm/', $Agent) && preg_match('/os/', $Agent))
			$os_Name = "IBM OS/2";
		else if (preg_match('/mac/', $Agent) && preg_match('/pc/', $Agent))
			$os_Name = "Macintosh";
		else if (preg_match('/powerpc/', $Agent))
			$os_Name = "PowerPC";
		elseif (preg_match('/aix/', $Agent))
			$os_Name = "AIX";
		else if (preg_match('/hpux/', $Agent))
			$os_Name = "HPUX";
		else if (preg_match('/netbsd/', $Agent))
			$os_Name = "NetBSD";
		else if (preg_match('/bsd/', $Agent))
			$os_Name = "BSD";
		else if (preg_match('/osfl/', $Agent))
			$os_Name = "OSF1";
		else if (preg_match('/irix/', $Agent))
			$os_Name = "IRIX";
		else if (preg_match('/freebsd/', $Agent))
			$os_Name = "FreeBSD";
		else
			$os_Name = "Unknown";

		return $os_Name;
	}

}
?>